﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using Medical.DataInterface;
using Medical.Entity;

namespace Medical.Data
{
    /// <summary>
    /// 文章属性值数据操作类
    /// </summary>
    public sealed class ArticleMetaValueAccessor:IArticleMetaValueAccessor
    {
        #region IArticleMetaValueAccessor 成员

        /// <summary>
        /// 获取文章属性值列表
        /// </summary>
        /// <param name="nArticleID"></param>
        /// <returns></returns>
        public DataTable GetList(int nType,int nArticleID)
        {
            return SqlHelper.ExecuteDataset(
                 SqlHelper.database,
                 CommandType.Text,
                 "select m.[ID] [ArticleMetaID],m.[Name],v.[ID],v.[MetaValue] from [ArticleMeta] m left join [ArticleMetaValue] v on m.[ID]=v.[ArticleMetaID] where m.[Type]=@Type and ([ArticleID]=@ArticleID or [ArticleID] is null) order by m.[OrderNum]",
                 new SqlParameter("@Type", nType),
                 new SqlParameter("@ArticleID", nArticleID)).Tables[0];
        }

        /// <summary>
        /// 获取文章属性值列表
        /// </summary>
        /// <param name="nArticleID"></param>
        /// <returns></returns>
        public DataTable GetList(int nType, string strArticleIDList)
        {
            return SqlHelper.ExecuteDataset(
                 SqlHelper.database,
                 CommandType.Text,
                 "select m.[ID] [ArticleMetaID],m.[Name],v.[ID],v.[MetaValue],v.[ArticleID] from [ArticleMeta] m left join [ArticleMetaValue] v on m.[ID]=v.[ArticleMetaID] where m.[Type]=@Type and ([ArticleID] in(" + strArticleIDList + ") or [ArticleID] is null) order by m.[OrderNum]",
                 new SqlParameter("@Type", nType)).Tables[0];
        }

        #endregion

        #region IBaseAccessor<ArticleMetaValue> 成员

        public ArticleMetaValue Get(int nID)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 新增属性值
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool Insert(ArticleMetaValue Info)
        {
            string strSql = "insert into [ArticleMetaValue]([ArticleMetaID],[ArticleID],[MetaValue]) values(@ArticleMetaID,@ArticleID,@MetaValue)";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@ArticleMetaID", Info.ArticleMetaID));
            parms.Add(new SqlParameter("@ArticleID", Info.ArticleID));
            parms.Add(new SqlParameter("@MetaValue", Info.MetaValue));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 编辑属性值
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool Update(ArticleMetaValue Info)
        {
            string strSql = "update [ArticleMetaValue] set [MetaValue]=@MetaValue where [ID]=@ID";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@MetaValue", Info.MetaValue));
            parms.Add(new SqlParameter("@ID", Info.ID));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        public bool Delete(int nID)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
